
package com.plian.system.service.pm.property.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.*;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.constant.yth.YthConstant;
import com.plian.system.dto.pm.property.PropertyInfoDTO;
import com.plian.system.entity.pm.property.*;
import com.plian.system.entity.sys.Org;
import com.plian.system.mapper.pm.property.PropertyChangeMapper;
import com.plian.system.mapper.pm.property.PropertyMapper;
import com.plian.system.properties.GzwProperties;
import com.plian.system.service.pm.property.*;
import com.plian.system.service.sys.file.FileService;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.service.transfer.ITransferFlowService;
import com.plian.system.vo.pm.property.PropertyVO;
import com.plian.system.vo.yth.YthResult;
import com.plian.system.wrapper.pm.property.PropertyWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *  服务实现类
 *
 * @author jianglei
 * @since 2019-12-05
 */
@Slf4j
@Service
@AllArgsConstructor
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements IPropertyService {

    private IContributorService contributorService;

    private IShareholdingService shareholdingService;

    private IPartnerOutboundService partnerOutboundService;

    private IPartnerSituationService partnerSituationService;

    private IPropertyBasicsService propertyBasicsService;

    private IContributorBasicsService contributorBasicsService;

    private IShareholdingBasicsService shareholdingBasicsService;

    private IPartnerOutboundBasicsService partnerOutboundBasicsService;

    private IPartnerSituationBasicsService partnerSituationBasicsService;

    private IPropertyRecordService propertyRecordService;

    private IContributorRecordService contributorRecordService;

    private IShareholdingRecordService shareholdingRecordService;

    private IPartnerOutboundRecordService partnerOutboundRecordService;

    private IPartnerSituationRecordService partnerSituationRecordService;

    private ITransferFlowService transferFlowService;

    private GzwProperties gzwProperties;

    private PropertyWrapper propertyWrapper;

    private IOrgService orgService;

    private PropertyChangeMapper propertyChangeMapper;

    private FileService fileService;

    @Override
    public String checkList(PropertyInfoDTO propertyInfoDTO, String competentOrganization) {
        List<Property> propertyList = propertyInfoDTO.getPropertyList();
        if (CollectionUtil.isEmpty(propertyList)){
            return "数据为空!";
        }

        Property gzwProperty = findByOrgId(competentOrganization);
        if (gzwProperty == null){
            return  "该账号所属国资委对应产权数据未配置!";
        }
        String gzwId = gzwProperty.getId();
        return check(gzwId, propertyInfoDTO, propertyList, competentOrganization, String.valueOf(FormStatusConstant.FINISH), 1);
    }

    private String check(String parentId, PropertyInfoDTO propertyInfoDTO, List<Property> propertyList, String competentOrganization, String status, int checkType){
        List<PropertyBasics> existPropertyList = propertyBasicsService.list(new LambdaQueryWrapper<PropertyBasics>()
                .eq(PropertyBasics::getStatus, FormStatusConstant.FINISH).eq(PropertyBasics::getCompetentOrganization, competentOrganization));
        List<String> existNames = Optional.ofNullable(existPropertyList).orElse(new ArrayList<>()).stream().map(PropertyBasics::getOrgName).collect(Collectors.toList());
        List<Property> noApprovalList = list(new LambdaQueryWrapper<Property>().eq(Property::getCompetentOrganization, competentOrganization).ne(Property::getStatus, FormStatusConstant.FINISH));
        existNames.addAll(Optional.ofNullable(noApprovalList).orElse(new ArrayList<>()).stream().map(Property::getOrgName).collect(Collectors.toList()));
        List<Property> transferDataList = list(new LambdaQueryWrapper<Property>()
                .eq(Property::getDataSource, TokenUtil.getTokenUserId())
                .in(Property::getStatus, Stream.of(String.valueOf(FormStatusConstant.UNREVIEWED),
                        String.valueOf(FormStatusConstant.REJECT)).collect(Collectors.toList())));
        Map<String, Property> transferDataMap = transferDataList.stream()
                .filter(property -> StringUtil.isNotBlank(property.getDataSourceId()))
                .collect(Collectors.toMap(Property::getDataSourceId, property -> property));
        List<String> addNames = new ArrayList<>();
        List<String> existIds = new ArrayList<>();
        StringBuilder message = new StringBuilder();

        Date now = new Date();
        for (Property property : propertyList){
            String orgName = property.getOrgName();
            boolean flag = true;
            if (CollectionUtil.isNotEmpty(transferDataMap) &&
                    transferDataMap.containsKey(property.getDataSourceId())){
                property.setId(transferDataMap.get(property.getDataSourceId()).getId());
                flag = false;
            }
            if (flag && existNames.contains(orgName)){
                if (message.length() > 0){
                    message.append(",");
                }
                message.append(orgName + "在系统中已存在");
                continue;
            }
            if (flag && addNames.contains(orgName)){
                if (message.length() > 0){
                    message.append(",");
                }
                message.append(orgName + "重复,");
                continue;
            }
            if (StringUtil.isNotBlank(property.getId())){
                existIds.add(property.getId());
            }
            if (StringUtil.isNotBlank(property.getDataSourceId())){
                existIds.add(property.getDataSourceId());
            }
            addNames.add(orgName);

            if (StringUtil.isBlank(property.getParentId())){
                property.setParentId(parentId);
            }
            if (StringUtil.isBlank(property.getId())){
                property.setCreateTime(now);
            }
            if (property.getUpdateTime() == null){
                property.setUpdateTime(property.getCreateTime());
            }

            property.setIsPrivateMix(0);
            if (StringUtil.isNotBlank(property.getActualCapital5())){
                BigDecimal bigDecimal = BigDecimalUtil.toBigdecimal(property.getActualCapital5());
                if (bigDecimal.compareTo(new BigDecimal(0)) == 1){
                    property.setIsPrivateMix(1);
                }
            }

            property.setStatus(status);
            property.setFormType(String.valueOf(FormTypeConstant.PM_PROPERTY_FORM));
            property.setCompetentOrganization(competentOrganization);
        }
        message.append(PropertyHelper.checkContributor(propertyInfoDTO.getContributorList(), existIds, checkType));
        message.append(PropertyHelper.checkShareholding(propertyInfoDTO.getShareholdingList(), existIds, checkType));
        message.append(PropertyHelper.checkPartnerOutbound(propertyInfoDTO.getPartnerOutboundList(), existIds, checkType));
        message.append(PropertyHelper.checkPartnerSituation(propertyInfoDTO.getPartnerSituationList(), existIds, checkType));
        return message.toString();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean saveList(PropertyInfoDTO propertyInfoDTO) {
        List<Property> propertyList = propertyInfoDTO.getPropertyList();
        List<Contributor> contributorList = propertyInfoDTO.getContributorList();
        List<Shareholding> shareholdingList = propertyInfoDTO.getShareholdingList();
        List<PartnerOutbound> partnerOutboundList = propertyInfoDTO.getPartnerOutboundList();
        List<PartnerSituation> partnerSituationList = propertyInfoDTO.getPartnerSituationList();
        String tokenUserId = TokenUtil.getTokenUserId();
        for (Property property : propertyList){
            property.setDataSource(tokenUserId);
        }
        boolean result = saveBatch(propertyList);
        propertyBasicsService.saveBatch(propertyList.stream().map(property -> {
            PropertyBasics propertyBasics = new PropertyBasics();
            BeanUtils.copyProperties(property, propertyBasics);
            return propertyBasics;
        }).collect(Collectors.toList()));

        propertyRecordService.saveBatch(propertyList.stream().map(property -> {
            PropertyRecord propertyRecord = new PropertyRecord();
            BeanUtils.copyProperties(property, propertyRecord);
            propertyRecord.setInitialId(property.getId());
            return propertyRecord;
        }).collect(Collectors.toList()));

        List<String> propertyIds = propertyList.stream().map(Property::getId).collect(Collectors.toList());

        contributorService.removeByPropertyId(propertyIds);
        contributorBasicsService.removeByPropertyId(propertyIds);
        contributorRecordService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(contributorList)){
            contributorService.saveBatch(contributorList);
            contributorBasicsService.saveBatch(contributorList.stream().map(contributor -> {
                ContributorBasics contributorBasics = new ContributorBasics();
                BeanUtils.copyProperties(contributor, contributorBasics);
                return contributorBasics;
            }).collect(Collectors.toList()));
            contributorRecordService.saveBatch(contributorList.stream().map(contributor -> {
                ContributorRecord contributorRecord = new ContributorRecord();
                BeanUtils.copyProperties(contributor, contributorRecord);
                return contributorRecord;
            }).collect(Collectors.toList()));
        }

        shareholdingService.removeByPropertyId(propertyIds);
        shareholdingBasicsService.removeByPropertyId(propertyIds);
        shareholdingRecordService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(shareholdingList)){
            shareholdingService.saveBatch(shareholdingList);
            shareholdingBasicsService.saveBatch(shareholdingList.stream().map(shareholding -> {
                ShareholdingBasics shareholdingBasics = new ShareholdingBasics();
                BeanUtils.copyProperties(shareholding, shareholdingBasics);
                return shareholdingBasics;
            }).collect(Collectors.toList()));
            shareholdingRecordService.saveBatch(shareholdingList.stream().map(shareholding -> {
                ShareholdingRecord shareholdingRecord = new ShareholdingRecord();
                BeanUtils.copyProperties(shareholding, shareholdingRecord);
                return shareholdingRecord;
            }).collect(Collectors.toList()));
        }

        partnerOutboundService.removeByPropertyId(propertyIds);
        partnerOutboundBasicsService.removeByPropertyId(propertyIds);
        partnerOutboundRecordService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(partnerOutboundList)){
            partnerOutboundService.saveBatch(partnerOutboundList);
            partnerOutboundBasicsService.saveBatch(partnerOutboundList.stream().map(partnerOutbound -> {
                PartnerOutboundBasics partnerOutboundBasics = new PartnerOutboundBasics();
                BeanUtils.copyProperties(partnerOutbound, partnerOutboundBasics);
                return partnerOutboundBasics;
            }).collect(Collectors.toList()));
            partnerOutboundRecordService.saveBatch(partnerOutboundList.stream().map(partnerOutbound -> {
                PartnerOutboundRecord partnerOutboundRecord = new PartnerOutboundRecord();
                BeanUtils.copyProperties(partnerOutbound, partnerOutboundRecord);
                return partnerOutboundRecord;
            }).collect(Collectors.toList()));
        }

        partnerSituationBasicsService.removeByPropertyId(propertyIds);
        partnerSituationService.removeByPropertyId(propertyIds);
        partnerSituationRecordService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(partnerSituationList)){
            partnerSituationService.saveBatch(partnerSituationList);
            partnerSituationBasicsService.saveBatch(partnerSituationList.stream().map(partnerSituation -> {
                PartnerSituationBasics partnerSituationBasics = new PartnerSituationBasics();
                BeanUtils.copyProperties(partnerSituation, partnerSituationBasics);
                return partnerSituationBasics;
            }).collect(Collectors.toList()));
            partnerSituationRecordService.saveBatch(partnerSituationList.stream().map(partnerSituation -> {
                PartnerSituationRecord partnerSituationRecord = new PartnerSituationRecord();
                BeanUtils.copyProperties(partnerSituation, partnerSituationRecord);
                return partnerSituationRecord;
            }).collect(Collectors.toList()));
        }
        return result;
    }

    @Override
    public Property findByOrgId(String orgId) {
        return getOne(new QueryWrapper<Property>().eq("org_id", orgId));
    }

    @Override
    public String unitCheckList(PropertyInfoDTO propertyInfoDTO, String competentOrganization) {
        List<Property> propertyList = propertyInfoDTO.getPropertyList();
        if (CollectionUtil.isEmpty(propertyList)){
            return "数据为空!";
        }

        String tokenOrgId = TokenUtil.getTokenOrgId();
        Property parentProperty = findByOrgId(tokenOrgId);
        if (parentProperty == null){
            return  "该账号所属单位对应产权数据未配置!";
        }
        String parentId = parentProperty.getId();
        return check(parentId, propertyInfoDTO, propertyList, competentOrganization, String.valueOf(FormStatusConstant.UNREVIEWED), 2);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean unitSaveList(PropertyInfoDTO propertyInfoDTO) {
        List<Property> propertyList = propertyInfoDTO.getPropertyList();
        List<Contributor> contributorList = propertyInfoDTO.getContributorList();
        List<Shareholding> shareholdingList = propertyInfoDTO.getShareholdingList();
        List<PartnerOutbound> partnerOutboundList = propertyInfoDTO.getPartnerOutboundList();
        List<PartnerSituation> partnerSituationList = propertyInfoDTO.getPartnerSituationList();
        String tokenUserId = TokenUtil.getTokenUserId();
        boolean result = saveOrUpdateBatch(propertyList);

        //先保存 然后在对数据更新父节点数据
        String competentOrganization = TokenUtil.getCompetentOrganization();
        List<PropertyBasics> propertyBasicsList = propertyBasicsService.list(new LambdaQueryWrapper<PropertyBasics>()
                .eq(PropertyBasics::getStatus, FormStatusConstant.FINISH).eq(PropertyBasics::getCompetentOrganization, competentOrganization));
        List<Property> noApprovalList = list(new LambdaQueryWrapper<Property>().eq(Property::getCompetentOrganization, competentOrganization).ne(Property::getStatus, FormStatusConstant.FINISH));
        Map<String, String> orgCodeIdMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(propertyBasicsList)){
            for (PropertyBasics propertyBasics : propertyBasicsList){
                orgCodeIdMap.put(propertyBasics.getOrgCode(), propertyBasics.getId());
            }
        }
        if (CollectionUtil.isNotEmpty(noApprovalList)){
            for (Property property : noApprovalList){
                orgCodeIdMap.put(property.getOrgCode(), property.getId());
            }
        }
        String tokenOrgId = TokenUtil.getTokenOrgId();
        Property parentProperty = findByOrgId(tokenOrgId);
        for (Property property : propertyList){
            property.setFillOrgId(tokenOrgId);
            property.setDataSource(tokenUserId);
            if (CollectionUtil.isNotEmpty(orgCodeIdMap) && orgCodeIdMap.containsKey(property.getCapitalOrgCode())){
                property.setParentId(orgCodeIdMap.get(property.getCapitalOrgCode()));
            }else {
                property.setParentId(parentProperty.getId());
            }
        }
        updateBatchById(propertyList);

        List<String> propertyIds = propertyList.stream().map(Property::getId).collect(Collectors.toList());
        Map<String, Property> dataSourceIdPropertyMap = propertyList.stream().collect(Collectors.toMap(Property::getDataSourceId, property -> property));

        contributorService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(contributorList)){
            List<Contributor> saveList = new ArrayList<>();
            for (Contributor contributor : contributorList){
                if (dataSourceIdPropertyMap.containsKey(contributor.getPropertyDataSourceId())){
                    contributor.setPropertyId(dataSourceIdPropertyMap.get(contributor.getPropertyDataSourceId()).getId());
                    saveList.add(contributor);
                }
            }
            contributorService.saveBatch(saveList);
        }

        shareholdingService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(shareholdingList)){
            List<Shareholding> saveList = new ArrayList<>();
            for (Shareholding shareholding : shareholdingList){
                if (dataSourceIdPropertyMap.containsKey(shareholding.getPropertyDataSourceId())){
                    shareholding.setPropertyId(dataSourceIdPropertyMap.get(shareholding.getPropertyDataSourceId()).getId());
                    saveList.add(shareholding);
                }
            }
            shareholdingService.saveBatch(saveList);
        }

        partnerOutboundService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(partnerOutboundList)){
            List<PartnerOutbound> saveList = new ArrayList<>();
            for (PartnerOutbound partnerOutbound : partnerOutboundList){
                if (dataSourceIdPropertyMap.containsKey(partnerOutbound.getPropertyDataSourceId())){
                    partnerOutbound.setPropertyId(dataSourceIdPropertyMap.get(partnerOutbound.getPropertyDataSourceId()).getId());
                    saveList.add(partnerOutbound);
                }
            }
            partnerOutboundService.saveBatch(saveList);
        }

        partnerSituationService.removeByPropertyId(propertyIds);
        if (CollectionUtil.isNotEmpty(partnerSituationList)){
            List<PartnerSituation> saveList = new ArrayList<>();
            for (PartnerSituation partnerSituation : partnerSituationList){
                if (dataSourceIdPropertyMap.containsKey(partnerSituation.getPropertyDataSourceId())){
                    partnerSituation.setPropertyId(dataSourceIdPropertyMap.get(partnerSituation.getPropertyDataSourceId()).getId());
                    saveList.add(partnerSituation);
                }
            }
            partnerSituationService.saveBatch(saveList);
        }

        if (CollectionUtil.isNotEmpty(propertyIds)){
            transferFlowService.submit(propertyIds, FormTypeConstant.PM_PROPERTY_FORM);
        }
        return result;
    }

    @Override
    public String ythSubmitBatch(String data) {
        YthResult ythResult = new YthResult();
        //成功的id集合
        List<String> successIds = new ArrayList<>();
        //失败的id集合
        List<String> failIds = new ArrayList<>();
        //失败的原因
        List<String> failReasons = new ArrayList<>();
        //将json字符串转换成list
        List<PropertyVO> dataList = JSON.parseArray(data, PropertyVO.class);
        if (CollectionUtil.isEmpty(dataList)){
            failReasons.add("数据为空");
            ythResult.setFailReason(StringUtils.join(failReasons, StringPool.COMMA));
            return JSON.toJSONString(ythResult);
        }
        //已登记的数据
        List<PropertyBasics> propertyBasicsList = propertyBasicsService.list(Wrappers.<PropertyBasics>lambdaQuery()
                .eq(PropertyBasics::getStatus, FormStatusConstant.FINISH)
                .isNotNull(PropertyBasics::getOrgCode)
                .eq(PropertyBasics::getCompetentOrganization, gzwProperties.getId()));
        Map<String, PropertyBasics> propertyBasicsMap = propertyBasicsList.stream()
                .collect(Collectors.toMap(PropertyBasics::getOrgCode, Function.identity()));
        Set<String> orgCodeList = propertyBasicsList.stream().map(PropertyBasics::getOrgCode).collect(Collectors.toSet());
        List<Property> propertyList = list(Wrappers.<Property>query().lambda()
                .eq(Property::getStatus, FormStatusConstant.FLOW).eq(Property::getCompetentOrganization, gzwProperties.getId()));
        List<PropertyChange> propertyChangeList = propertyChangeMapper.selectList(Wrappers.<PropertyChange>query().lambda()
                .ne(PropertyChange::getStatus, FormStatusConstant.FINISH).eq(PropertyChange::getCompetentOrganization, gzwProperties.getId()));
        if (CollectionUtil.isEmpty(propertyList)){
            orgCodeList.addAll(propertyList.stream().map(Property::getOrgCode).collect(Collectors.toSet()));
        }
        if (CollectionUtil.isEmpty(propertyChangeList)){
            orgCodeList.addAll(propertyChangeList.stream().map(PropertyChange::getOrgCode).collect(Collectors.toSet()));
        }
        //查询获取可修改的老数据
        List<Property> oldList = list(new LambdaQueryWrapper<Property>()
                .isNotNull(Property::getOrgCode)
                .eq(Property::getCompetentOrganization, gzwProperties.getId())
                .in(Property::getStatus, Stream.of(FormStatusConstant.STASH,
                        FormStatusConstant.UNREVIEWED, FormStatusConstant.REJECT).collect(Collectors.toList())));
        Map<String, Property> oldOrgCodeMap = oldList.stream().collect(Collectors.toMap(Property::getOrgCode, Function.identity()));
        List<Org> orgList = orgService.getYthOrgList();
        Map<String, Org> dataSourceIdOrgMap = orgList.stream().collect(Collectors.toMap(Org::getDataSourceId, Function.identity()));
        //出资人情况
        List<Contributor> contributorList = new ArrayList<>();
        //合伙人登记情况
        List<PartnerSituation> partnerSituationList = new ArrayList<>();
        //对外投资情况
        List<PartnerOutbound> partnerOutboundList = new ArrayList<>();
        //境外控股人
        List<Shareholding> shareholdingList = new ArrayList<>();
        List<String> removeChildIds = new ArrayList<>();
        Date now = new Date();
        for (PropertyVO propertyVO : dataList){
            if (orgCodeList.contains(propertyVO.getOrgCode())){
                failIds.add(propertyVO.getDataSourceId());
                failReasons.add(propertyVO.getDataSourceId() + "该企业已经登记，不能再登记或修改");
                continue;
            }
            if (!propertyBasicsMap.containsKey(propertyVO.getCapitalOrgCode())){
                failIds.add(propertyVO.getDataSourceId());
                failReasons.add(propertyVO.getDataSourceId() + "父节点不存在");
                continue;
            }
            if (successIds.contains(propertyVO.getDataSourceId())){
                failIds.add(propertyVO.getDataSourceId());
                failReasons.add(propertyVO.getDataSourceId() + "id重复");
                continue;
            }
            if (StringUtil.isBlank(propertyVO.getNature())){
                failIds.add(propertyVO.getDataSourceId());
                failReasons.add(propertyVO.getDataSourceId() + "nature 性质不能为空");
                continue;
            }

            successIds.add(propertyVO.getDataSourceId());
            Property entity = propertyWrapper.voToEntity(propertyVO);
            if (oldOrgCodeMap.containsKey(propertyVO.getOrgCode())){
                entity.setId(oldOrgCodeMap.get(propertyVO.getOrgCode()).getId());
                removeChildIds.add(entity.getId());
            }
            if (dataSourceIdOrgMap.containsKey(entity.getOrgId())){
                entity.setOrgId(dataSourceIdOrgMap.get(entity.getOrgId()).getId());
            }
            if (dataSourceIdOrgMap.containsKey(entity.getFillOrgId())){
                entity.setFillOrgId(dataSourceIdOrgMap.get(entity.getFillOrgId()).getId());
            }
            entity.setCreateTime(now);
            entity.setUpdateTime(now);
            entity.setFormType(String.valueOf(FormTypeConstant.PM_PROPERTY_FORM));
            entity.setDataSource(YthConstant.DATA_NINGBO);
            entity.setCompetentOrganization(gzwProperties.getId());
            entity.setStatus(String.valueOf(FormStatusConstant.UNREVIEWED));
            entity.setParentId(propertyBasicsMap.get(propertyVO.getCapitalOrgCode()).getId());
            saveOrUpdate(entity);
            saveFile(entity, propertyVO);
            switch (entity.getNature()){
                case "0":
                    //境内企业
                    if (CollectionUtil.isNotEmpty(propertyVO.getContributorList())){
                        for (Contributor contributor : propertyVO.getContributorList()){
                            contributor.setPropertyId(entity.getId());
                        }
                        contributorList.addAll(propertyVO.getContributorList());
                    }
                    break;
                case "1":
                    //境外企业
                    if (CollectionUtil.isNotEmpty(propertyVO.getContributorList())){
                        for (Contributor contributor : propertyVO.getContributorList()){
                            contributor.setPropertyId(entity.getId());
                        }
                        contributorList.addAll(propertyVO.getContributorList());
                    }
                    if (CollectionUtil.isNotEmpty(propertyVO.getShareholdingList())){
                        for (Shareholding shareholding : propertyVO.getShareholdingList()){
                            shareholding.setPropertyId(entity.getId());
                        }
                        shareholdingList.addAll(propertyVO.getShareholdingList());
                    }
                    break;
                case "2":
                    //合伙企业
                    if (CollectionUtil.isNotEmpty(propertyVO.getPartnerSituationList())){
                        for (PartnerSituation partnerSituation : propertyVO.getPartnerSituationList()){
                            partnerSituation.setPropertyId(entity.getId());
                        }
                        partnerSituationList.addAll(propertyVO.getPartnerSituationList());
                    }
                    if (CollectionUtil.isNotEmpty(propertyVO.getPartnerOutboundList())){
                        for (PartnerOutbound partnerOutbound : propertyVO.getPartnerOutboundList()){
                            partnerOutbound.setPropertyId(entity.getId());
                        }
                        partnerOutboundList.addAll(propertyVO.getPartnerOutboundList());
                    }
                    break;
                default:
            }
        }
        if (CollectionUtil.isNotEmpty(removeChildIds)){
            contributorService.remove(Wrappers.<Contributor>lambdaQuery()
                    .in(Contributor::getPropertyId, removeChildIds));
            shareholdingService.remove(Wrappers.<Shareholding>lambdaQuery()
                    .in(Shareholding::getPropertyId, removeChildIds));
            partnerSituationService.remove(Wrappers.<PartnerSituation>lambdaQuery()
                    .in(PartnerSituation::getPropertyId, removeChildIds));
            partnerOutboundService.remove(Wrappers.<PartnerOutbound>lambdaQuery()
                    .in(PartnerOutbound::getPropertyId, removeChildIds));
        }

        if (CollectionUtil.isNotEmpty(contributorList)){
            contributorService.saveBatch(contributorList);
        }
        if (CollectionUtil.isNotEmpty(shareholdingList)){
            shareholdingService.saveBatch(shareholdingList);
        }
        if (CollectionUtil.isNotEmpty(partnerSituationList)){
            partnerSituationService.saveBatch(partnerSituationList);
        }
        if (CollectionUtil.isNotEmpty(partnerOutboundList)){
            partnerOutboundService.saveBatch(partnerOutboundList);
        }


        ythResult.setSuccessIds(successIds);
        ythResult.setFailIds(failIds);
        ythResult.setFailReason(StringUtils.join(failReasons, StringPool.COMMA));
        //ythResult转换成json字符串
        return JSON.toJSONString(ythResult);
    }

    private void saveFile(Property entity, PropertyVO propertyVO){
        fileService.upYthFile(propertyVO.getF1(), propertyVO.getId(), "f1");
        fileService.upYthFile(propertyVO.getF2(), propertyVO.getId(), "f2");
        fileService.upYthFile(propertyVO.getF3(), propertyVO.getId(), "f3");
        fileService.upYthFile(propertyVO.getF4(), propertyVO.getId(), "f4");
        fileService.upYthFile(propertyVO.getF5(), propertyVO.getId(), "f5");
        fileService.upYthFile(propertyVO.getF6(), propertyVO.getId(), "f6");
        fileService.upYthFile(propertyVO.getF7(), propertyVO.getId(), "f7");
        fileService.upYthFile(propertyVO.getF8(), propertyVO.getId(), "f8");
        fileService.upYthFile(propertyVO.getF9(), propertyVO.getId(), "f9");
    }

    @Override
    public Map<String, String> getSourceId(){
        List<Property> propertyList = list(new LambdaQueryWrapper<Property>().eq(Property::getDataSource, 3));
        return propertyList.stream().collect(Collectors.toMap(Property::getDataSourceId, Property::getId));
    }

}
